package com.wx.user.controller;



import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wx.commons.entity.Org;
import com.wx.commons.model.OrgSaveReq;
import com.wx.user.service.OrgService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
  * (org)表控制层
  * @author liuwenxuan
  * @TableName org
  * @createDate 20250801.01
  */
@RestController
@RequestMapping("org")
public class OrgController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private OrgService orgService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param org 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Org> page, Org org) {
        return success(this.orgService.page(page, new QueryWrapper<>(org)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.orgService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param org 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Org org) {
        return success(this.orgService.save(org));
    }

    /**
     * 修改数据
     *
     * @param org 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Org org) {
        return success(this.orgService.updateById(org));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.orgService.removeByIds(idList));
    }

    @GetMapping("/tree")
     public R tree() {
        List<Org> orgs = orgService.list();
        List<Org> firstOrgs = orgs.stream().filter(org -> org.getPid() == 0).collect(Collectors.toList());
        List<Org> orgRes = bfs(firstOrgs,orgs);
        return success(orgRes);
    }

    private List<Org> bfs(List<Org> firstOrgs, List<Org> orgs) {
        if (firstOrgs.size()==0) return firstOrgs;
        for (Org org : firstOrgs) {
            org.setChildren(orgs.stream().filter(org1 -> org1.getPid().equals(org.getId())).collect(Collectors.toList()));
            bfs(org.getChildren(),orgs);
        }
        return firstOrgs;
    }

    @PostMapping("/batch")
    public R batch(@RequestBody OrgSaveReq orgSaveReq) {
        if (CollectionUtil.isNotEmpty(orgSaveReq.getDel())){
            List<Long> orgIds = orgSaveReq.getDel().stream().map(Org::getId).collect(Collectors.toList());
            this.orgService.removeByIds(orgIds);
        }
        if (CollectionUtil.isNotEmpty(orgSaveReq.getInsertAndUpdate())){
            this.orgService.saveOrUpdateBatch(orgSaveReq.getInsertAndUpdate());
        }
        return success(true);
    }
}

